home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / fade into you / getting there / WWW / MacHTTP Extras / Decode URL / Decode URL.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  3KB  |  106 lines

  1. /********************************************
  2.  * Decode URL OSAX - by Chuck Shotton, 10/17/94
  3.  * This code, the OSAX, and all of its constituent parts are hereby
  4.  * placed in the public domain.
  5.  *
  6.  * This code demonstrates how to build an OSAX for use with MacHTTP
  7.  * and AppleScript. This particular example decodes %xx encodings in
  8.  * form args and URLs passed as strings to it.
  9.  *
  10.  * This code was written using Think C, but should build with no
  11.  * problems under CodeWarrior as well. It should be build as a 68k
  12.  * code resource to avoid problems. The code resource type is 'osax'
  13.  * and its resource ID MUST match the event class and code.
  14.  * For this event the name is "AEVTaevtdURL", indicating that this is
  15.  * an AppleEvent, class aevt, code dURL.
  16.  *********************************************/
  17.  
  18. #include <AppleEvents.h> 
  19.  
  20. /*********************************************/
  21.  
  22. unsigned char HexChar(unsigned char c)
  23. {
  24. unsigned char k;
  25.     /*upper case the character*/
  26.     
  27.     if (c>='a' && c<='z') c = (c-'a')+'A';
  28.     k=0;
  29.     if (c>='0' && c<='9')
  30.         k = c - '0';
  31.     else if (c>='A' && c<='Z')
  32.         k = (c-'A') + (unsigned char) 10;
  33.     return k;
  34. }
  35.  
  36. /*********************************************/
  37.  
  38. pascal OSErr main(    AppleEvent *theAEEvent, 
  39.                             AppleEvent *theReply, 
  40.                             long *theRefCon) 
  41.  
  42. {     
  43.  
  44.     /* Function Prototypes */
  45.      
  46.     /* variables */
  47.     OSErr            theErr = noErr;     
  48.     DescType         typeCode;
  49.     Size             sizeOfParam,
  50.                     actualSize;
  51.     char *url;
  52.     int i;
  53.     unsigned char bite;
  54.     
  55.     theErr = AESizeOfParam(    theAEEvent,
  56.                                keyDirectObject,
  57.                                &typeCode,
  58.                                &sizeOfParam);
  59.   
  60.     if (theErr != noErr){            
  61.         return theErr;
  62.     }     
  63.     else{  
  64.         /*try converting lots of types of strings into something we can use*/
  65.         if ((typeCode == typeChar) || (typeCode == typeStyledText) || 
  66.             (typeCode == typeIntlText)) {        
  67.         
  68.             /*Make a place to store the parameter, then get it.*/
  69.             if (url = (char *) NewPtr (sizeOfParam+2)) {
  70.                 theErr = AEGetParamPtr(theAEEvent, keyDirectObject, typeChar, 
  71.                                          &typeCode, (Ptr) url, 
  72.                                         sizeOfParam+1, &actualSize);
  73.                                         
  74.                 if(theErr == noErr) {
  75.                     /* 'C' string has a null as last char */
  76.                     url [actualSize] = '\0';
  77.                     i=0;
  78.                     
  79.                     /*decode the %xx encodings*/
  80.                     while ( url[i] ) {
  81.                         while ( url[i] ) {
  82.                             if (url [i]=='%' && i+2<actualSize) {
  83.                                 bite = (HexChar(url[i+1])*16 + HexChar(url[i+2])) & 0x7F;
  84.                                 url [i] = bite;
  85.                                 BlockMove (&url[i+3], &url[i+1], actualSize - (i+2));
  86.                                 actualSize -= 2;
  87.                             }
  88.                             i++;
  89.                         }
  90.                     }
  91.                 }
  92.                 /*return the result of any processing to the caller*/
  93.                 if (theReply->descriptorType != typeNull) 
  94.                     theErr = AEPutParamPtr(theReply, keyDirectObject, typeChar,
  95.                         url, actualSize );
  96.                 DisposPtr (url);
  97.             }
  98.             else return errAEEventNotHandled;
  99.         }
  100.         else // Wasn't a string so bail
  101.             return errAEEventNotHandled;
  102.     } 
  103.         
  104.     return theErr;
  105.  }
  106.